package com.rrx.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rrx.model.Order;
import com.rrx.service.OrederService;
import com.rrx.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2018/8/15.
 */

@Controller
public class OrderController {
    @Resource
    OrederService orederService;
    ObjectMapper mapper = new ObjectMapper();

    /**
     * 添加订单
     * @param request
     * @return
     */
    @RequestMapping("/wxAddOrder")
    @ResponseBody
    public String addOrder(HttpServletRequest request){
        int userId= Integer.parseInt(request.getParameter("userId"));
        Date date=new Date();
        String name=request.getParameter("name");
        String  number= String.valueOf(date.getTime()+3);
        String faulttype=request.getParameter("faulttype");
        String faultinfo=request.getParameter("faultinfo");
        String address=request.getParameter("region");
        if(address!=null){
        Order order=new Order(userId,number,name,address,faulttype,faultinfo,date);
            boolean bool=orederService.addOrder(order);
            if (!bool) {
                number="err";
            }
        }
        else{
            number="err";
        }
        return  number;
    }

    /**
     * 查询单个订单
     * @param request
     * @return
     */
    @RequestMapping("/wxSelectOrder")
    @ResponseBody
    public String selectOrder(HttpServletRequest request) {
        String  orderId= request.getParameter("orderId");
        Order order=orederService.selectOrder(orderId);
        String  json= null;
        try {
            json = mapper.writeValueAsString(order);
            System.out.println("json"+json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有订单
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/wxSelectAllOrder")
    @ResponseBody
    public String selectAllOrder() throws JsonProcessingException {
        List<Order> list=orederService.selectAllOrder();
        String json="";
        json=mapper.writeValueAsString(list);
        return json;
    }

    /**
     * 接受订单
     * @param request
     * @return
     */
    @RequestMapping("/wxTakeOrder")
    @ResponseBody
    public String takkOrder (HttpServletRequest request){
        int fettlerId=Integer.parseInt(request.getParameter("fettlerId"));
        String orderId=request.getParameter("orderId");
        if(orederService.takeorder(fettlerId,orderId)){
            return orderId;
        }
        return "false";
    }
    /**
     * 用户确认订单完成
     */
    @RequestMapping("/wxCompleteOrder")
    @ResponseBody
    public String completeOrder(HttpServletRequest request){
        String orderId=request.getParameter("orderId");
        if(orederService.completedorder(orderId)){
            return orderId;
        }
        return "false";
    }


    @RequestMapping("/getOrders")
    @ResponseBody
    public String getOrders(HttpServletRequest request) {
        String requestPage = request.getParameter("page");
        String requestType = request.getParameter("type");
        System.out.println(requestType);
        int page = 0;
        int type = -1;
        try {
            page = (Integer.parseInt(requestPage) - 1) * 10;
        }catch(Exception e) {}
        try {
            type = Integer.parseInt(requestType);
        }catch(Exception e) {}
        String json = orederService.getOrdersLimited(page, type);
        System.out.println(json);
        return json;
    }

    @RequestMapping("/getOrdersByStatus")
    @ResponseBody
    public String getOrdersByStatus(HttpServletRequest request) {
        String requestUserid = request.getParameter("userid");
        String requestStatus = request.getParameter("status");
        int userid = -1;
        int status = 0;
        try {
            userid = Integer.parseInt(requestUserid);
        }catch(Exception e) {
            return "{\"err\":\"wrongId\"}";
        }
        try {
            status = Integer.parseInt(requestStatus);
        }catch(Exception e){}
        String json = orederService.getOrderByStatus(userid, status);
        return json;
    }
}
